<?php
/**
 * 广告管理模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class AdAction extends CommonAction {
	
	public function index(){
		$map=$this->_search();
		$model=M('Ad');
		$map['position_id']=intval($_REQUEST['position_id']);
		$this->_list($model,$map);
		$this->display();
	}
	
	public function add() {
		$model = D('Ad');
		//广告位置
		$Position=M('AdPosition');
		$map['id']=intval($_REQUEST['position_id']);
		$this->assign('pos',$Position->where($map)->find());
		//打开方式
        $this->assign('target',$model->getTarget());
		$this->display();
	}
	
	public function insert(){
		$model = D('Ad');
		if (false === $model->create()) {
			$this->error($model->getError());
		}
		//若有上传文件
		if(!empty($_FILES['image']['name'])){
			//图片上传 自定义目录
			$info=$this->_upload(UPLOAD_PATH.'/'.MODULE_NAME.'/',false);
			//图片存入
			$model->image = $image = isset($info[0]['savename']) ? $info[0]['savepath'].$info[0]['savename']: '';
		}else{
			$this->error('请上传图片');
		}
		//保存数据
		if (false !== $record_id=$model->add()) {
			//保存附件信息
			$Attach=M("Attach");
			$data=array(
					'record_id'	=> 	$record_id,
					'path'		=> 	$image,
					'model' 	=> 	MODULE_NAME,
					'size'		=> 	getFileSize($image),
					'add_time'	=>	time()
			);
			if(isset($image)){
				$Attach->data($data)->add();
			}
			$this->success('添加成功!',$this->getReturnUrl().'&position_id='.$_GET['position_id']);
		} else {
			$this->error('添加失败!');
		}
	}
	
	public function edit() {
        $model = D('Ad');
        //广告位置
        $Position=M('AdPosition');
        $map['id']=intval($_REQUEST['position_id']);
        $this->assign('pos',$Position->where($map)->find());
        //打开方式
        $this->assign('target',$model->getTarget());
        //当前数据
        $id = $_REQUEST[$model->getPk()];
        $data = $model->find($id);
        $this->assign('data', $data);
        $this->display('add');
    }
	
    public function update(){
    	$model = D('Ad');
    	//create与save中间不能用model操作数据库
    	$ad=$model->getById($_POST['id']);
    
    	if (false === $model->create()) {
    		$this->error($model->getError());
    	}
    	//若有上传信息
    	if(!empty($_FILES['image']['name'])){
    		$info=$this->_upload(UPLOAD_PATH.'/'.MODULE_NAME.'/',false);
    		if(isset($info[0]['savename'])){
    			//成功后删除原来图片
    			@unlink($ad['image']);
    			$model->image = $image = $info[0]['savepath'].$info[0]['savename'];
    		}else{
    			$model->image = $image=	$ad['image'];
    		}
    	}else{
    		$model->image = $image=$ad['image'];
    	}
    	// 更新数据
    	if (false !== $model->save()) {
    		//更新附件信息
    		$Attach=M("Attach");
    		$data['record_id']=intval($_POST['id']);
    		$data['path']=$image;
    		$data['model']=MODULE_NAME;
    		$data['size']=getFileSize($image);
    		$data['add_time']=time();
    		if(isset($image)&&$image!=$ad['image']){
    			//删除原附件
    			if($this->_delAttach($_POST['id'])){
    				//插入新附件
    				$Attach->data($data)->add();
    			}
    		}
    		$this->success('编辑成功!',$this->getReturnUrl().'&position_id='.$_GET['position_id']);
    	} else {
    		$this->error('编辑失败!');
    	}
    }
    
    //ajax删除广告操作
    public function ajaxDelete() {
    	//删除指定记录
    	$model = D("Ad");
    	if (!empty($model)) {
    		$pk = $model->getPk();
    		$id = $_REQUEST ['id'];
    		if (isset($id)) {
    			$condition = array(
    					$pk => array('in', explode(',', $id)),
    			);
    			if (false !== $model->where($condition)->delete()) {
    				//删除附件
    				$Attach=M("Attach");
    				$condition=array(
    						'record_id' => array('in', explode(',', $id)),
    						'model'=>MODULE_NAME
    				);
    				$images=$Attach->where($condition)->select();
    				if(count($images)>0){
    					foreach ($images as $v){
    						@unlink($v['path']);
    					}
    				}
    				$Attach->where($condition)->delete();
    				$this->ajaxReturn(1,'删除成功！',1);
    			} else {
    				$this->ajaxReturn(0,'删除失败！',0);
    			}
    		} else {
    			$this->ajaxReturn(0,'非法操作！',0);
    		}
    	}
    	$this->forward();
    }
}

?>